import {observable, extendObservable, action, toJS} from 'mobx';
import {getDoorRecordInfo} from 'util/api';
import _ from 'lodash';
import {validateFields} from '../Common.js';
import {message} from 'antd';
import store from 'store';

//导出
export default class HouseholdStore {
  constructor() {
    this.reset(true);
  }

  @action
  reset = init => {
    const state = {
      tableData: [],
      loading: false,
      tableParams: {},
      openType: '',
      name: '',
      startTime: '',
      endTime: '',
      recordLocation: '',
    };

    if (init) {
      extendObservable(this, state);
    } else {
      Object.keys(state).forEach(key => (this[key] = state[key]));
    }
  };

  @action
  onWillMount = async (location, match, history) => {
    this.reset();

    this.loading = true;
    await this.getDoorRecordInfo();
    this.loading = false;
  };
  // @action
  // getDataListInfo = async (params = {}) => {
  //   const res = await getDataListInfo(params);
  //   if (res.result === 'success') {
  //     let list = res.data.apartmentList;
  //     this.apartmentList = list.map(item => {
  //       return {name: item.blockAndUnit, value: item.unitId};
  //     });
  //   } else {
  //     message.error(res.msg);
  //   }
  // };
  //
  @action
  getDoorRecordInfo = async (
    params = {
      pageNo: 1,
      pageSize: 10,
      openType: '',
      recordLocation: '',
      name: '',
      deviceId: '',
      startTime: '',
      endTime: '',
    },
  ) => {
    this.loading = true;
    const res = await getDoorRecordInfo(params);
    this.loading = false;
    if (res.result === 'success') {
      this.tableParams = {...this.tableParams, ...params};
      this.tableData = res;
    } else {
      message.error(res.msg);
    }
  };
  @action
  changeRecordLocation = async pos => {
    this.my_recordLocation = pos;
  };
  @action
  changeName = async name => {
    this.my_name = name;
  };
  @action
  changeOpenType = async openType => {
    this.my_openType = openType;
  };
  @action
  changeRangePicker = async date => {
    this.my_date = date;
  };
  @action
  search = async () => {
    let openType = this.my_openType;
    let name = this.my_name;
    let startTime = this.my_date ? this.my_date[0] : '';
    let endTime = this.my_date ? this.my_date[1] : '';
    let recordLocation = this.my_recordLocation;
    this.openType = openType;
    this.recordLocation = recordLocation;
    this.name = name;
    this.startTime = startTime;
    this.endTime = endTime;
    let params = {
      pageNo: 1,
      pageSize: 10,
      ...this.tableParams,
      openType,
      name,
      startTime,
      endTime,
      recordLocation,
    };
    this.getDoorRecordInfo(params);
  };
}
